using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Microsoft.Win32;

namespace VSColorsToSqlServer
{
    class ColorImporter
    {
        public enum ImportMethod
        {
            VS2005ToSQL2005,
            VS2005ToSQL2008,

            VS2008ToSQL2005,
            VS2008ToSQL2008
        }

        private const string VS2005Key = @"Software\Microsoft\VisualStudio\8.0\FontAndColors\{A27B4E24-A735-4D1D-B8E7-9716E1E3D8E0}";
        private const string VS2008Key = @"Software\Microsoft\VisualStudio\9.0\FontAndColors\{A27B4E24-A735-4D1D-B8E7-9716E1E3D8E0}";

        private const string Sql2005Key = @"Software\Microsoft\Microsoft SQL Server\90\Tools\Shell\FontAndColors\{A27B4E24-A735-4D1D-B8E7-9716E1E3D8E0}";
        private const string Sql2008Key = @"Software\Microsoft\Microsoft SQL Server\100\Tools\Shell\FontAndColors\{A27B4E24-A735-4D1D-B8E7-9716E1E3D8E0}";

        private IDictionary<string, string> _map;
        private IList<string> _ignorePrefixes;

        public ColorImporter()
        {
            _map = new Dictionary<string, string>();

            #region values we map from one to the other
            _map.Add("String Background", "SQL String Background");
            _map.Add("String FontFlags", "SQL String FontFlags");
            _map.Add("String Foreground", "SQL String Foreground");

            _map.Add("Operator Foreground", "SQL Operator Foreground");
            _map.Add("Operator Background", "SQL Operator Background");
            _map.Add("Operator FontFlags", "SQL Operator FontFlags");

            _map.Add("User Types Foreground", "SQL Stored Procedure Foreground");
            _map.Add("User Types Background", "SQL Stored Procedure Background");
            _map.Add("User Types FontFlags", "SQL Stored Procedure FontFlags");

            _map.Add("User Types (Interfaces) Foreground", "SQL System Function Foreground");
            _map.Add("User Types (Interfaces) Background", "SQL System Function Background");
            _map.Add("User Types (Interfaces) FontFlags", "SQL System Function FontFlags");

            _map.Add("User Types (Value types) Foreground", "SQL System Table Foreground");
            _map.Add("User Types (Value types) Background", "SQL System Table Background");
            _map.Add("User Types (Value types) FontFlags", "SQL System Table FontFlags");

            _map.Add("XML Attribute Foreground", "XmlAttribute Foreground");
            _map.Add("XML Attribute Background", "XmlAttribute Background");
            _map.Add("XML Attribute FontFlags", "XmlAttribute FontFlags");

            _map.Add("XML Attribute Quotes Foreground", "XmlAttributeQuotes Foreground");
            _map.Add("XML Attribute Quotes Background", "XmlAttributeQuotes Background");
            _map.Add("XML Attribute Quotes FontFlags", "XmlAttributeQuotes FontFlags");

            _map.Add("XML CData Section Foreground", "XmlCData Foreground");
            _map.Add("XML CData Section Background", "XmlCData Background");
            _map.Add("XML CData Section FontFlags", "XmlCData FontFlags");

            _map.Add("XML Comment Foreground", "XmlComment Foreground");
            _map.Add("XML Comment Background", "XmlComment Background");
            _map.Add("XML Comment FontFlags", "XmlComment FontFlags");

            _map.Add("XML Delimiter Foreground", "XmlDelimiter Foreground");
            _map.Add("XML Delimiter Background", "XmlDelimiter Background");
            _map.Add("XML Delimiter FontFlags", "XmlDelimiter FontFlags");

            _map.Add("XML Keyword Foreground", "XmlKeyword Foreground");
            _map.Add("XML Keyword Background", "XmlKeyword Background");
            _map.Add("XML Keyword FontFlags", "XmlKeyword FontFlags");

            _map.Add("XML Name Foreground", "XmlName Foreground");
            _map.Add("XML Name Background", "XmlName Background");
            _map.Add("XML Name FontFlags", "XmlName FontFlags");

            _map.Add("XML Processing Instruction Foreground", "XmlProcessingInstruction Foreground");
            _map.Add("XML Processing Instruction Background", "XmlProcessingInstruction Background");
            _map.Add("XML Processing Instruction FontFlags", "XmlProcessingInstruction FontFlags");

            _map.Add("XML Text Foreground", "XmlText Foreground");
            _map.Add("XML Text Background", "XmlText Background");
            _map.Add("XML Text FontFlags", "XmlText FontFlags");

            _map.Add("XSLT Keyword Foreground", "XsltKeyword Foreground");
            _map.Add("XSLT Keyword Background", "XsltKeyword Background");
            _map.Add("XSLT Keyword FontFlags", "XsltKeyword FontFlags");

            #endregion

            _ignorePrefixes = new string[] {
            "CSS ", "Disassembly ", "HTML ", "Refactoring ", "XML Doc "
         };

        }

        public void Copy(ImportMethod method)
        {
            string vsSrcKey = VS2005Key;
            string sqlDstKey = Sql2005Key;
            switch (method)
            {
                case ImportMethod.VS2005ToSQL2008:
                    sqlDstKey = Sql2008Key;
                    break;

                case ImportMethod.VS2008ToSQL2005:
                    vsSrcKey = VS2008Key;
                    break;

                case ImportMethod.VS2008ToSQL2008:
                    vsSrcKey = VS2008Key;
                    sqlDstKey = Sql2008Key;
                    break;
            }

            RegistryKey src = Registry.CurrentUser.OpenSubKey(vsSrcKey);
            RegistryKey dst = Registry.CurrentUser.OpenSubKey(sqlDstKey, true);

            string[] values = src.GetValueNames();
            foreach (string valname in values)
            {
                CopyValue(valname, src, dst);
            }
        }

        private void CopyValue(string valname, RegistryKey src, RegistryKey dst)
        {
            if (!Ignore(valname))
            {
                string rv = valname;
                if (_map.ContainsKey(valname))
                    rv = _map[valname];

                dst.SetValue(rv, src.GetValue(valname));
                // hack: we also copy the Identifier ones over to plain text
                if (valname.StartsWith("Identifier") && !valname.Contains("Background"))
                {
                    dst.SetValue(
                       valname.Replace("Identifier", "Plain Text"),
                       src.GetValue(valname)
                       );
                }
            }
        }

        private bool Ignore(string valname)
        {
            foreach (string prefix in _ignorePrefixes)
            {
                if (valname.StartsWith(prefix))
                    return true;
            }
            return false;
        }
    }

    public partial class Main : Form
    {
        public Main()
        {
            InitializeComponent();
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void btnImport_Click(object sender, EventArgs e)
        {
            ColorImporter importer = new ColorImporter();
            ColorImporter.ImportMethod method = ColorImporter.ImportMethod.VS2008ToSQL2008;
            if (rbVS2005.Checked)
                method = rbSQL2005.Checked ? ColorImporter.ImportMethod.VS2005ToSQL2005 : ColorImporter.ImportMethod.VS2005ToSQL2008;
            else if (rbSQL2005.Checked)
                method = ColorImporter.ImportMethod.VS2008ToSQL2005;

            const string msgFormat = "Import Font and Color schemas from {0} to {1} now?";
            string msg = string.Format(msgFormat,
                rbVS2005.Checked ? rbVS2005.Text : rbVS2008.Text,
                rbSQL2005.Checked ? rbSQL2005.Text : rbSQL2008.Text);

            if (DialogResult.Yes != MessageBox.Show(msg, "Confirm your choice", MessageBoxButtons.YesNo))
                return;

            try
            {
                importer.Copy(method);
                MessageBox.Show("Font and Color settings imported successfully", "Font and Color settings imported successfully", MessageBoxButtons.OK);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Error", MessageBoxButtons.OK);
            }
        }
    }
}